#include "gamecontrol.h"
#include "gamedefine.h"

GameControl* GameControl::instance = nullptr;

GameControl::GameControl()
{
    this->setParent(Widget::widget);
}

void GameControl::BGMove()
{
    mbackGround2.moveBy(0,2);
    mbackGround1.moveBy(0,2);//moveBy 移动量 dx--x方向移动量 dy--y方向移动量

    if(mbackGround1.y() >=mbackGround1.pixmap().height())
    {
        mbackGround1.setY(-mbackGround1.pixmap().height());
    }
    else if(mbackGround2.y() >=mbackGround1.pixmap().height())
    {
        mbackGround2.setY(-mbackGround2.pixmap().height());
    }
}

void GameControl::GameInit()
{
    //设置视图的父亲为窗口
    mGameView.setParent(Widget::widget);
    LoadStartScene();//开始场景初始化

    //定时器初始化
    mBGMoveTimer=new QTimer(this);
    mPlaneMoveTimer = new QTimer(this);
    mPlaneShootTimer = new QTimer(this);
    mBulletMoveTimer = new QTimer(this);
    mEnemyCreateTimer = new QTimer(this);
    mEnemyMoveTimer = new QTimer(this);

    //绑定定时器处理函数
    connect(mBGMoveTimer,&QTimer::timeout,this,&GameControl::BGMove);
    connect(mPlaneMoveTimer,&QTimer::timeout,this,&GameControl::PlaneMove);
    connect(mPlaneShootTimer,&QTimer::timeout,this,&GameControl::PlaneBulletShoot);
    connect(mEnemyCreateTimer,&QTimer::timeout,this,&GameControl::CreateEnemy);
    connect(mBulletMoveTimer,&QTimer::timeout,[this](){
        //子弹移动
        for(auto bullet : mBulletList)
        {
            bullet->BulletMove();
        }
        //碰撞检测
        Collision();
    });
    connect(mEnemyMoveTimer,&QTimer::timeout,[this](){
          for(auto enemy : mEnemyList)
          {
              enemy->EnemyMove();
          }
    });
}

void GameControl::LoadStartScene()
{
    mStartScene.setSceneRect(QRect(0,0,512,768));//游戏开始场景大小
    mStartScene.addPixmap(QPixmap(":/Image/Image/2978cb64b1852b9dce3f7311ecd7d2f.jpg"));

    auto startBtn = new QToolButton();
    startBtn->setAutoRaise(true);
    startBtn->setIcon(QIcon(":/Image/Image/91d2df23a7039eaed5854daaf5f2305.jpg"));//开始游戏图标
    startBtn->setIconSize(QSize(171,51));
    startBtn->move(256-171/2,384);
    //开始游戏点击
    connect(startBtn,&QToolButton::clicked,[this](){

        //加载游戏场景
        this->LoadGameScene();
        //开始游戏
        this->GameStart();

    });
    mStartScene.addWidget(startBtn);

    //设置当前场景为开始场景
    mGameView.setScene(&mStartScene);
    mGameView.show();

}

void GameControl::LoadGameScene()
{
    mGameScene.setSceneRect(QRect(0,0,512,768));//游戏进行场景大小
    mbackGround1.setPixmap(QPixmap(":/Image/Image/img_bg_level_1.jpg"));//与QImage用法类似
    mbackGround2.setPixmap(QPixmap(":/Image/Image/img_bg_level_1.jpg"));
    mPlane.setPixmap(QPixmap(":/Image/Image/hero2.png"));

    //设置元素位置
     //mPlane.setPos(100,100);
     mbackGround2.setPos(0,-mbackGround2.pixmap().height());

     //图片元素添加到场景
     mGameScene.addItem(&mbackGround1);
     mGameScene.addItem(&mbackGround2);
     mGameScene.addItem(&mPlane);

     //设置当前场景为游戏场景
     mGameView.setScene(&mGameScene);
     mGameView.show();

     /*
             //----------------------播放音效
             this->mMediaBG = new QMediaPlayer(this);
             //注意 Qurl路径对象 字符串格式 "qrc:/ + 前缀名+ /路径"
             this->mMediaBG->setMedia(Quel("qrc"));
             this->mMediaBG->play();

     */

}

void GameControl::GameStart()
{
    //开启定时器

    mBGMoveTimer->start(GameDefine::BackgroundUpdateTime);
    mPlaneMoveTimer->start(GameDefine::PlayerMoveUpdateTime);
    mPlaneShootTimer->start(GameDefine::PlaneShootUpdateTime);
    mBulletMoveTimer->start(GameDefine::BulletMoveUpdateTime);
    mEnemyCreateTimer->start(GameDefine::EnemyCreateTime);
    mEnemyMoveTimer->start(GameDefine::EnemyMoveUpdateTime);

}

void GameControl::GameOver()
{
    //结束逻辑
    //...


}

void GameControl::PlaneMove()
{
    for(int keyCode : mKeyList)
    {
        switch(keyCode)
        {
        case Qt::Key_W: mPlane.moveBy(0,-1*mPlane.MoveSpeed());break;
        case Qt::Key_S: mPlane.moveBy(0,1*mPlane.MoveSpeed());break;
        case Qt::Key_A: mPlane.moveBy(-1*mPlane.MoveSpeed(),0);break;
        case Qt::Key_D: mPlane.moveBy(1*mPlane.MoveSpeed(),0);break;
        }
    }
    //边界判断
    if(mPlane.x() < 0)
    {
        mPlane.setX(0);
    }
    if(mPlane.y()< 0)
    {
        mPlane.setY(0);
    }
    if(mPlane.x() > GameDefine::ScreenWidth - mPlane.pixmap().width())
    {
        mPlane.setX(GameDefine::ScreenWidth - mPlane.pixmap().width());
    }
    if(mPlane.y() > GameDefine::ScreenHeight - mPlane.pixmap().height())
    {
        mPlane.setY(GameDefine::ScreenHeight - mPlane.pixmap().height());
    }



}

void GameControl::PlaneBulletShoot()
{
    //创建子弹
   QPixmap bulletImg(":/Image/Image/hero_bullet_7.png");
   QPoint pos(mPlane.x()+mPlane.pixmap().width()/2 ,mPlane.y());
   Bullet* bullet =new Bullet(pos,bulletImg,Bullet::BT_Player);

   //添加到场景
   mGameScene.addItem(bullet);

   //添加到子弹管理器
   mBulletList.append(bullet);
}

void GameControl::CreateEnemy()
{
    QPixmap pixmap(":/Image/Image/img-plane_7.png");
    int randx = qrand()%(512-pixmap.width());//敌机随机出现
    Enemy* enemy =new Enemy(QPoint(randx,-200),pixmap);

    //添加到场景
    mGameScene.addItem(enemy);

    //添加到管理器
    mEnemyList.append(enemy);
}

void GameControl::Collision()
{
    //遍历子弹
    for(int i = 0;i < mBulletList.size();i++)
    {
        //遍历敌机
        for(int j = 0;j < mEnemyList.size();j++)
        {
            if(mBulletList[i]->collidesWithItem(mEnemyList[j]))//碰撞检测
            {
                //移除场景   (实际的对象并没有被删除)
               mGameScene.removeItem(mBulletList[i]);
               mGameScene.removeItem(mEnemyList[j]);

               //移除管理器
               mBulletList.removeOne(mBulletList[i]);
               mEnemyList.removeOne(mEnemyList[j]);
            }
        }
    }
}

